package com.rjwh.dingdong.client.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class UtilAndroid {

    public final static int IMAGE_PROPORTION = 0;
    public final static int IMAGE_EQUALITY = 1;
    public final static int IMAGE_THREEQUARTERSOF = 2;
    public final static int IMAGE_NONE = -1;

    public static String armToString(String voiceFilePath) throws IOException {
        File file = new File(voiceFilePath);
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
            byte[] b = new byte[input.available()];// input.available()�����ļ��ֽڳ���
            input.read(b);// ���ļ������ݶ��뵽������ȥ
            input.close();
            return Base64.encodeToString(b, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 保存图片文件到SD卡
     *
     * @param @param context
     * @param @param oldFile
     * @return void
     * @throws
     * @Title: savePicToFile
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public static void savePicToFile(final Context context, File oldFile) {

        final File file = new File(FileUtils.getPicStorageDirectory());
        final String fileName = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(System.currentTimeMillis()) + ".jpg";
        final File mediaFile = new File(file.getPath() + File.separator
                + fileName);
        new AsyncTask<File, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(File... params) {
                File oldFile = params[0];
                if (oldFile.exists()) { // 文件存在时
                    int bytesum = 0;
                    int byteread = 0;
                    try {
                        FileInputStream inStream = new FileInputStream(
                                oldFile.getPath()); // 读入原文件
                        FileOutputStream fs = new FileOutputStream(
                                mediaFile.getPath());
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((byteread = inStream.read(buffer)) != -1) {
                            bytesum += byteread; // 字节数 文件大小
                            // System.out.println(bytesum);
                            fs.write(buffer, 0, byteread);
                        }
                        fs.flush();
                        fs.close();
                        inStream.close();
                        //把文件插入到系统图库
                        MediaStore.Images.Media.insertImage(
                                context.getContentResolver(),
                                mediaFile.getAbsolutePath(), fileName, null);
                        // 最后通知图库更新
                        context.sendBroadcast(new Intent(
                                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
                                .parse("file://" + mediaFile)));
                        return true;
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
                return false;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Boolean result) {
                if (result) {
                    ToastUtil.showToast(context, "已保存到" + file.getPath()
                            + "文件夹");
                } else {
                    ToastUtil.showToast(context, "保存出错");
                }
                super.onPostExecute(result);
            }

        }.executeOnExecutor(TheradPoolUtil.getInstant().getExecutor(), oldFile);

    }
}
